package com.fyt.video.project;

import com.fyt.video.ClientDemo.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.springframework.util.StringUtils;

public class VideoClient {
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    boolean bRealPlay;//是否在预览.
    NativeLong lUserID =new  NativeLong(0);//用户句柄
    String m_sDeviceIP;//已登录设备的IP地址
    HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo;//设备信息
    HCNetSDK.NET_DVR_IPPARACFG m_strIpparaCfg;//IP参数

    /**
     * 初始化资源配置
     */
    public int initDevices() {
        if (!hCNetSDK.NET_DVR_Init()) {
            return 1;       //初始化失败
        }
        return 0;
    }

    /**
     * 设备注册
     */
    public int deviceRegister(String name, String password, String ip, String port) {
        if (bRealPlay) {//判断是否在预览
            return 2;//"注册新用户请先停止当前预览";
        }
        if (lUserID.longValue() > -1) {//先注销，在登录
            hCNetSDK.NET_DVR_Logout_V30(lUserID);
            lUserID = new NativeLong(-1);
        }
        //注册
        m_sDeviceIP = ip;
        m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();//获取设备参数结构
        lUserID = hCNetSDK.NET_DVR_Login_V30(m_sDeviceIP, (short) Integer.parseInt("8000"), name, password, m_strDeviceInfo);
        long userID = lUserID.longValue();
        if (userID == -1) {
            m_sDeviceIP = "";//登录未成功，IP设置为空
            return 3;
        }
        return 0;
    }

    /**
     * 获取设备通道
     */
    public int getChannelNumber() {
        IntByReference ibrBytesReturned = new IntByReference(0);//获取IP接入配置参数
        boolean bRet = false;
        int iChannelNum = -1;
        m_strIpparaCfg = new HCNetSDK.NET_DVR_IPPARACFG();
        m_strIpparaCfg.write();
        Pointer lpIpParaConfig = m_strIpparaCfg.getPointer();
        bRet = hCNetSDK.NET_DVR_GetDVRConfig(lUserID, HCNetSDK.NET_DVR_GET_IPPARACFG, new NativeLong(0), lpIpParaConfig, m_strIpparaCfg.size(), ibrBytesReturned);
        m_strIpparaCfg.read();
        String devices = "";
        if (!bRet) {
            //设备不支持，则表示没有IP通道
            System.out.println(m_strDeviceInfo.byChanNum);
            for (int iChannum = 0; iChannum < m_strDeviceInfo.byChanNum; iChannum++) {
                devices = "Camera" + (iChannum + m_strDeviceInfo.byStartChan);
            }
        } else {
            for (int iChannum = 0; iChannelNum < HCNetSDK.MAX_IP_CHANNEL; iChannum++) {
                if (m_strIpparaCfg.struIPChanInfo[iChannum].byEnable == 1) {
                    devices = "IPCamera" + (iChannum + m_strDeviceInfo.byStartChan);
                }
            }
        }

        if (!StringUtils.isEmpty(devices)) {
            if (devices.charAt(0) == 'C') {//Camara开头表示模拟通道
                //字符集串获取通道号
                iChannelNum = Integer.parseInt(devices.substring(6));
            } else {
                if (devices.charAt(0) == 'I') {//IPCamara开头表示IP通道
                    //子字符创中获取通道号,IP通道号要加32
                    iChannelNum = Integer.parseInt(devices.substring(8)) + 32;
                } else {
                    return 4;
                }
            }
        }
        return iChannelNum;
    }

    /**
     * 注销
     * */
    public void shutDownDev(){
        if (lUserID.longValue() > -1){
            hCNetSDK.NET_DVR_Logout_V30(lUserID);
        }
        hCNetSDK.NET_DVR_Cleanup();
    }
}
